package com.smartwebx.ha.dal.realtime.sqlprovider;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.time.DateUtils;

import com.smartwebx.ha.dal.realtime.dao.handler.ProcessDataEnum;
import com.smartwebx.ha.dal.usebean.SplitDatePointBean;

public class ProcessDataProvider {

	public String getWaterExceedWarnDaysSQL(Map<String, Object> maps) {
		Date endDate = (Date) maps.get("endDate");
		endDate = DateUtils.addHours(endDate, 23);
		endDate = DateUtils.addMinutes(endDate, 59);
		maps.put("endDate", endDate);

		ProcessDataEnum tabName = (ProcessDataEnum) maps.get("tabName");

		BEGIN();
		SELECT("COUNT(distinct to_char(TM,'yyyy/mm/dd')) COUNT");
		FROM(tabName.toString());
		WHERE("STCD = #{stcd}");
		WHERE("TM >= #{startDate}");
		WHERE("TM < #{endDate}");

		switch (tabName) {
		case HE_DAO:
			WHERE("Z IS NOT NULL");
			WHERE("Z >= #{warn}");
			break;
		case SHUI_KU:
			WHERE("RZ IS NOT NULL");
			WHERE("RZ >= #{warn}");
			break;
		case YAN_ZHA:
			WHERE("UPZ IS NOT NULL");
			WHERE("UPZ >= #{warn}");
			break;
		case BENG_ZHAN:
			WHERE("PPUPZ IS NOT NULL");
			WHERE("PPUPZ >= #{warn}");
			break;
		case CHAO_WEI:
			WHERE("TDZ IS NOT NULL");
			WHERE("TDZ >= #{warn}");
			break;
		default:
			break;
		}
		return SQL();
	}

	
	public String getProcessListSQL(Map<String, Object> maps) {
		SplitDatePointBean splitDatePoint = (SplitDatePointBean) maps
				.get("splitDatePointBean");
		ProcessDataEnum tabName = (ProcessDataEnum) maps.get("tabName");
		ProcessDataEnum dataType = null;
		if (maps.containsKey("dataType")) {
			dataType = (ProcessDataEnum) maps.get("dataType");
		}

		String operatType = splitDatePoint.getOperatType();

		Date now = new Date();
		Date nowDate = DateUtils.truncate(now, Calendar.DATE);
		int nowHour = ((Long) DateUtils.getFragmentInHours(nowDate,
				Calendar.DATE)).intValue();

		Date startDate = null, endDate = null;
		if ("127".indexOf(operatType) > -1) {
			startDate = nowDate;
			endDate = DateUtils.addHours(nowDate, nowHour + 1);
		} else if ("45".indexOf(operatType) > -1) {

			startDate = splitDatePoint.getStartDate();
			endDate = DateUtils.addHours(nowDate, nowHour + 1);
		} else if ("6".equals(operatType)) {
			startDate = splitDatePoint.getStartDate();
			endDate = splitDatePoint.getEndDate();
		} else if ("3".equals(operatType)) {
			Date startDateSplitDate = splitDatePoint.getStartDate();
			startDate = startDateSplitDate;
			endDate = DateUtils.addHours(startDateSplitDate, 23);
		}
		maps.put("startDate", startDate);
		maps.put("endDate", endDate);

		BEGIN();
		switch (tabName) {
		case ZHENG_FA:
			SELECT("TM,DYE val");
			WHERE("DYE IS NOT NULL");
			break;
		case DI_XIA:
			SELECT("TM,GWBD val");
			WHERE("GWBD IS NOT NULL");
			break;
		case BENG_ZHAN:
			if (dataType.equals(ProcessDataEnum.WATER)) {
				SELECT("TM,PPUPZ val");
				WHERE("PPUPZ IS NOT NULL");
			} else if (dataType.equals(ProcessDataEnum.FLOW)) {
				SELECT("TM,PMPQ val");
				WHERE("PMPQ IS NOT NULL");
			}
			break;
		case HE_DAO:
			if (dataType.equals(ProcessDataEnum.WATER)) {
				SELECT("TM,Z val");
				WHERE("Z IS NOT NULL");
			} else if (dataType.equals(ProcessDataEnum.FLOW)) {
				SELECT("TM,Q val");
				WHERE("Q IS NOT NULL");
			}
			break;
		case SHUI_KU:
			if (dataType.equals(ProcessDataEnum.WATER)) {
				SELECT("TM,RZ val");
				WHERE("RZ IS NOT NULL");
			} else if (dataType.equals(ProcessDataEnum.FLOW)) {
				SELECT("TM,INQ val");
				WHERE("INQ IS NOT NULL");
			}
			break;
		case CHAO_WEI:
			SELECT("TM,TDZ val");
			WHERE("TDZ IS NOT NULL");
			break;
		case YAN_ZHA:
			if (dataType.equals(ProcessDataEnum.WATER)) {
				SELECT("TM,UPZ val");
				WHERE("UPZ IS NOT NULL");
			} else if (dataType.equals(ProcessDataEnum.FLOW)) {
				SELECT("TM,TGTQ val");
				WHERE("TGTQ IS NOT NULL");
			}
			break;
		default:
			break;
		}

		FROM(tabName.toString());
		WHERE("STCD = #{stcd}");
		WHERE("TM >= #{startDate}");
		WHERE("TM < #{endDate}");

		return SQL();
	}
}
